<?php
session_start();
$currentUser = isset($_SESSION["user"])?$_SESSION["user"]:null;
if(!$currentUser){
    header("Location: login.php");
    die(0);
}

$pageSize = 8;
$pageIndex = isset($_GET["pageIndex"])?$_GET["pageIndex"]:null;
if($pageIndex<=0) $pageIndex = 1;

$queryTitle = isset($_GET["title"])?$_GET["title"]:null;
$queryAuthor = isset($_GET["author"])?$_GET["author"]:null;
$queryMinTime = isset($_GET["minTime"])?$_GET["minTime"]:null;
$queryMaxTime = isset($_GET["maxTime"])?$_GET["maxTime"]:null;

$where = "";
if($queryTitle) {
    $where = " `Title` like ('%$queryTitle%')";
}

if($queryAuthor){
    if($where) $where .= " AND `AuthorName` like ('%$queryAuthor%')"; 
    else $where .= "`AuthorName` like ('%$queryAuthor%')";   
}

if($queryMinTime){
    if($where) $where .= " AND "; 
    $where .= "createTime>='$queryMinTime'";   
}

if($queryMaxTime){
    if($where) $where .= " AND "; 
    $where .= "createTime<='$queryMaxTime'";   
}

if($where) $where = "WHERE $where";

$startRow = ($pageIndex -1)*$pageSize;

$sql = "select * from `article` $where order by `createTime` desc limit $startRow,$pageSize ";
$conn = new mysqli("localhost","root","","myblog");
$rs = $conn->query($sql);
if($conn->error) die($conn->error);
$rows=[];
while($row = $rs->fetch_assoc()){
    $rows[]=$row;
}
$rs->close();

$totalSql = "select count(Id) as total from `article` $where";
$rs = $conn->query($totalSql);
$total = $rs->fetch_assoc()['total'];

$pageCount = ceil($total/$pageSize);

function makePageUrl($pageIndex){
    $url = "training2.php?";
    foreach($_GET as $k =>$v){
        if($k==="pageIndex"){
            continue;
        }
        $url .=$k."=".$v."&";
    }
    $url.="pageIndex=".$pageIndex;
    return $url;
}
?>

<!DOCtype html>
<html>
    <head>
        <title>myBlog</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <link type="text/css" rel="stylesheet" href="training_c.css">
    </head>
    <body>
        <div class="container">
            <div class="col-5" id="header">
                <p class="col-4">我 的 博 客</p>
                <a href="logout.php">退出</a>
                <div class="col-1">当前用户:  <?=$currentUser["Username"]?></div>
            </div>
            <div class="col-5" id="menu">
                <div class="col-6" id="menu-1">
                    <ul class="col-6"><!--一级菜单-->
                        <li class="col-1">首页
                            <ul class="col-6"><!--二级菜单-->
                                <li class="col-6">查看文章</li>
                                <li class="col-6"><a href="article_add.php">添加文章</a></li>
                                <li class="col-6">热门文章</li>
                            </ul>
                        </li>
                        <li class="col-1">网站链接
                            <ul class="col-6"><!--二级菜单-->
                                <li class="col-6"><a href="https://www.bilibili.com">B站</a></li>
                                <li class="col-6"><a href="https://www.baidu.com/">百度</a></li>
                                <li class="col-6"><a href="https://www.taobao.com">淘宝</a></li>
                            </ul>
                        </li>
                        <li class="col-1">正在搭建...</li>
                        <li class="col-1">正在搭建...</li>
                        <li class="col-1">正在搭建...</li>
                    </ul>
                </div>
            </div>
            <div class="col-5" id="main">
                <div class="col-5" id="searchFor">
                    <form action="training2.php" method="get">
                    <span>
                        <label>标题</label>
                        <input type="text" value="<?=$queryTitle ?>" name="title" />
                    </span>
                    <span>
                        <label>作者</label>
                        <input type="text" value="<?=$queryAuthor ?>" name="author" />
                    </span>
                    <span>
                        <label>时间范围：</label>
                        从 <input type="text" value="<?=$queryMinTime ?>" name="minTime" />
                        到 <input type="text" value="<?=$queryMaxTime?>" name="maxTime" />
                    </span>
                    <input type="submit" value="搜索" id="btn" />
                    </form>
                </div>
                <div  class="col-5" id="tableTitle">
                    <p>文章列表</p>
                </div>
                <div class="col-5" id="table">
                    <?php if($rows) { ?>    
                    <?php foreach($rows as $row){ ?>
                        <div class="col-1" id="text">
                            <div id="title">
                                <a href="article_detail.php?Id=<?=$row["Id"]?>" title="<?=$row["Title"]?>"><span><?=$row["Title"]?></span></a>
                            </div>
                            <div id="author">作者：<?php echo $row["AuthorName"]?></div>
                            <div id="time">
                                创建时间：<?php echo $row["CreateTime"]?></br>
                                修改时间：<?php echo $row["UpdateTime"]?>
                            </div>
                            <div id="operate">
                                <a href="article_detail.php?Id=<?=$row["Id"]?>">详情</a>
                                <?php if($currentUser["Username"]===$row["AuthorName"]) {?>
                                <a href="article_modify.php?Id=<?=$row["Id"]?>"> 修改 </a>
                                删除
                                <?php }?>
                            </div>
                        </div>
                        <?php } ?>
                        <?php }else { ?>
                            <div id="noData">没有数据....</div>
                    <?php }?>
                </div>
                <div class="col-5" id="page">
                    共<?=$total?>条记录，共<?=$pageCount?>页   当前第<?=$pageIndex?>页  
                    <a href="training2.php?<?=makePageUrl(1)?>">首页</a><
                    <?php for($i=1;$i<=$pageCount;$i++) { ?>
                        <a class="<?=$pageIndex==$i?'currentPageNo':''?>"  href="training2.php?<?=makePageUrl($i)?>"><?=$i?></a>
                    <?php }?>
                    >
                    <a href="training2.php?<?=makePageUrl($pageCount)?>">尾页</a>           
                </div>
            </div>
            <div class="col-5" id="footer">
                <div col-4>
                    <a href="https://www.bilibili.com/video/BV1NZ4y1j7nw?p=2"><span>黑人抬棺</span></a>
                    <span>专业团队</span>
                    <span>诚信可靠</span>
                    <span>联系电话</span>
                    <span>8-8-8-8-8-8-8-8</span>
                </div>
            </div>
        </div>
    </body>
</html>